<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:func="http://exslt.org/functions" 
  xmlns:fres="http://xmlns.4suite.org/reserved"
  xmlns:a="http://xmlns.heustics.org/schema/myelin/0.1/"  
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/"
  xmlns:regex="http://exslt.org/regular-expressions" 
  xmlns:url="http://xmlns.heustics.org/xslt/ext/urllib/0.1/"
  extension-element-prefixes="func url"
  exclude-result-prefixes="xsl fres func a l regex url"
> 
  <!-- http://localhost:8080 -->  
  <!-- xsl:param name="fres:uri-path"/ -->
  
  <!-- xsl:param name="fres:xslt-path"/>
  <xsl:param name="fres:xslt-jail"/ -->
  
  <xsl:param name="fres:http-host"/>
  <xsl:param name="fres:config-doc"><XsltConfig/></xsl:param>
  <!-- xsl:param name="fres:http-protocol"/ -->
  
  <xsl:param name="fres:session-referrer"/>
  
  <xsl:param name="a:config" select="/*/a:config"/>
  
  <xsl:variable name="a:standalone-service" select="1"/>
  
  <xsl:variable name="a:resource-ext">
    <xsl:if test="$a:method != $a:config/@default-method">
      <xsl:value-of select="concat('.',$a:method)"/>
    </xsl:if>
  </xsl:variable>  
  
  <!-- variables -->
  <xsl:variable name="a:source-root" select="/*"/>  
  <xsl:variable name="a:node-id" select="string($a:source-root/@node-id)"/>
  <xsl:variable name="a:public-root" select="string($a:config/@public-root)"/>
  <xsl:variable name="a:public-root-length" select="string-length($a:public-root)"/>
  <xsl:variable name="a:root-url" select="concat(substring($a:config/@root-uri,string-length($a:config/@document-root)),$a:resource-ext)"/>  
  <xsl:variable name="a:root-path" select="string(regex:match($a:root-url,'(/.*/)+|(/)(.*)')[position()=3])"/>  
  
  <xsl:variable name="a:current-resource" select="string($a:source-root/@resource-uri)"/>
  <xsl:variable name="a:current-url-wo-ext" select="concat(substring($a:current-resource,$a:public-root-length))"/>  
  <xsl:variable name="a:current-url" select="concat($a:current-url-wo-ext,$a:resource-ext)"/>
  <xsl:variable name="a:method" select="string($a:source-root/@method)"/>
  
  <xsl:variable name="a:return-to" select="a:get-return-to()"/>
 
  <xsl:variable name="a:action" select="string($a:source-root/@action)"/>  
  <xsl:variable name="a:mode" select="string($a:source-root/@mode)"/>  
  
  <xsl:variable name="a:http-host" select="$fres:http-host"/>
  
  <xsl:variable name="a:head" select="$a:source-root/a:head"/>
  <xsl:variable name="a:account" select="$a:head/a:account"/> 
  <xsl:variable name="a:preferences" select="$a:account/a:preferences"/>
  <xsl:variable name="a:theme" select="string($a:preferences/@theme)"/>
  <xsl:variable name="a:locale" select="string($a:preferences/@locale)"/>  
  <xsl:variable name="a:timezone" select="string($a:preferences/@timezone)"/>
  <xsl:variable name="a:lang" select="substring-before($a:locale,'-')"/>
  
  <xsl:variable name="a:static-url" select="string($a:config/@static-url)"/>
  <xsl:variable name="a:static-root" select="string($a:config/@static-root)"/>
 
  <xsl:variable name="a:node-acls" select="$a:account/a:node-acls"/>
   
  <xsl:variable name="a:logged-in" select="$a:account/@logged-in"/>
   
  <xsl:variable name="a:session-url-param"/><!--
    <xsl:if test="$a:config/@using-url-sessions">
      <xsl:value-of select="concat('&amp;',$a:account/@session-key,'=',$a:account/@session-id)"/>
    </xsl:if>
  </xsl:variable -->
  
  <xsl:variable name="a:method-url-param"/>
  
  <xsl:variable name="a:preferences-url-params"/><!--   
    <xsl:if test="not($a:logged-in) and $a:theme != $a:config/@default-theme">
      <xsl:value-of select="concat('&amp;theme=',$a:theme)"/>
    </xsl:if>
    <xsl:if test="not($a:logged-in) and $a:locale != $a:config/@default-locale">
      <xsl:value-of select="concat('&amp;locale=',$a:locale)"/>
    </xsl:if>
    <xsl:if test="not($a:logged-in) and $a:timezone != $a:config/@default-timezone">
      <xsl:value-of select="concat('&amp;timezone=',$a:timezone)"/>
    </xsl:if>
  </xsl:variable -->
  
  <xsl:variable name="a:mode-param">
    <xsl:if test="$a:mode and $a:mode != $a:config/@default-mode">
      <xsl:value-of select="concat('&amp;mode=',$a:mode)"/>
    </xsl:if>
  </xsl:variable>
  
  <xsl:variable name="a:common-url-params" 
    select="$a:mode-param"/><!-- removed
      $a:method-url-param,
      $a:preferences-url-params,
      $a:session-url-param -->
  
  <func:function name="a:get-return-to">
    <xsl:choose>
      <xsl:when test="$fres:session-referrer">
        <func:result select="$fres:session-referrer"/>
      </xsl:when>
      <xsl:otherwise>
        <func:result select="a:format-root-action()"/>
      </xsl:otherwise>
    </xsl:choose>    
  </func:function>  
  
  <!-- TODO: need to encode params -->
  <func:function name="a:encode-url-param-chain">
    <xsl:param name="name"/>
    <xsl:param name="val"/>
    <xsl:param name="next"/>
    <func:result select="concat($name,'=',url:quote($val),'&amp;',$next)"/>
  </func:function>  
  
  <func:function name="a:encode-url-param">
    <xsl:param name="name"/>
    <xsl:param name="val"/>
    <func:result select="concat($name,'=',url:quote($val))"/>
  </func:function>
  
  <!-- TODO: check if there are params alreading in URL
             if no params, separator must be a '?' instead of '&'
             Must cases, there should already be params ...
  -->
  <func:function name="a:encode-url">
    <xsl:param name="url"/>
    <xsl:param name="params"/>
    <func:result select="concat($url,'&amp;',$params)"/>
  </func:function>
  
  <func:function name="a:normalize-url">
    <xsl:param name="url"/>
    <xsl:param name="params"/>
    <xsl:choose>
      <xsl:when test="string-length($params)">
        <xsl:choose>
          <xsl:when test="substring($params,1,1)='&amp;'">
            <func:result select="concat($url,'?',substring($params,2))"/>
          </xsl:when>
          <xsl:otherwise>
            <func:result select="concat($url,'?',$params)"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <func:result select="$url"/>
      </xsl:otherwise>
    </xsl:choose>
  </func:function>
    
  <func:function name="a:is-not-action">
    <xsl:param name="action"/>
    <func:result select="$action != $a:action"/>
  </func:function>
  
  <func:function name="a:is-not-current-node">
    <xsl:param name="node"/>
    <func:result select="$node != $a:node-id"/>
  </func:function>
  
  <!-- uri to public url conversions -->
  <func:function name="a:public-url">
    <xsl:param name="uri"/>     
    <func:result select="concat(substring($uri,$a:public-root-length),$a:resource-ext)"/>    
  </func:function>
  
  <func:function name="a:public-url-wo-ext">
    <xsl:param name="uri"/>     
    <func:result select="substring($uri,$a:public-root-length)"/>    
  </func:function>
  
  <func:function name="a:public-static-url">
    <xsl:param name="uri"/>     
    <func:result select="concat($a:static-url,substring($uri,string-length($a:static-root)+1))"/>    
  </func:function>
  
  <func:function name="a:public-url-full">
    <xsl:param name="uri"/>     
    <func:result select="concat('http://',$a:http-host,substring($uri,$a:public-root-length),$a:resource-ext)"/>    
  </func:function>
  
   <func:function name="a:public-url-full-secure">
    <xsl:param name="uri"/>     
    <func:result select="concat($a:config/@https,$a:http-host,substring($uri,$a:public-root-length),$a:resource-ext)"/>    
  </func:function>
  
  <func:function name="a:format-resource-extension">
    <xsl:param name="uri"/>     
    <func:result select="concat($uri,$a:resource-ext)"/>    
  </func:function>
  
  <func:function name="a:format-url-file-ext">
    <xsl:param name="url"/>
    <xsl:param name="method" select="$a:method"/>
    <xsl:choose>
      <xsl:when test="$method != $a:config/@default-method">
        <func:result select="concat($url,'.',$method)"/>  
      </xsl:when>
      <xsl:otherwise>
        <func:result select="$url"/>  
      </xsl:otherwise>
    </xsl:choose>
    
  </func:function>
  
  <func:function name="a:format-action">
    <xsl:param name="a"/>
    <xsl:if test="$a != $a:config/@default-action">
      <func:result select="concat('&amp;action=',$a)"/>
    </xsl:if>
  </func:function>
  
  <func:function name="a:format-theme">
    <xsl:param name="a"/>
    <xsl:if test="not($a:logged-in) and $a != $a:config/@default-theme">
      <func:result select="concat('&amp;theme=',$a)"/>
    </xsl:if>
  </func:function>
  
  <func:function name="a:format-locale">
    <xsl:param name="a"/>
    <xsl:if test="not($a:logged-in) and $a != $a:config/@default-locale">
      <func:result select="concat('&amp;locale=',$a)"/>
    </xsl:if>
  </func:function>
  
  <func:function name="a:format-timezone">
    <xsl:param name="a"/>
    <xsl:if test="$a and not($a:logged-in) and $a != $a:config/@default-timezone">
      <func:result select="concat('&amp;timezone=',url:quote($a))"/>
    </xsl:if>
  </func:function>
  
  <func:function name="a:format-method">
    <xsl:param name="a"/>
    <xsl:if test="$a != $a:config/@default-method">
      <func:result select="concat('&amp;method=',$a)"/>
    </xsl:if>
  </func:function>
  
  <func:function name="a:format-mode">
    <xsl:param name="a"/>
    <xsl:if test="$a != $a:config/@default-mode">
      <func:result select="concat('&amp;mode=',$a)"/>
    </xsl:if>
  </func:function>
  
  <!-- URL format functions -->  
  <func:function name="a:format-url-min">
    <xsl:param name="url"/>
    <xsl:param name="action" select="$a:config/@default-action"/>
    <func:result
      select="a:normalize-url($url,
        a:format-action($action)
      )"/>
  </func:function>
  
   <func:function name="a:format-url-full-secure">
    <xsl:param name="url"/>
    <func:result
      select="concat($a:config/@https,$a:http-host,$url)"/>
  </func:function>
  
  <func:function name="a:format-url">
    <xsl:param name="url"/>
    <xsl:param name="action" select="$a:config/@default-action"/>  
    <func:result
      select="a:normalize-url($url,concat(
        a:format-action($action),
        $a:common-url-params
      ))"/>
  </func:function>
  
  <func:function name="a:format-url-session-timeout">
    <xsl:param name="action" select="'login'"/>
    <xsl:param name="url" select="$a:current-url"/>    
    <func:result
      select="a:normalize-url($url,concat(
        a:format-action($action),
        $a:method-url-param,
        $a:preferences-url-params
      ))"/>
  </func:function>
    
  <func:function name="a:format-root-request">  
    <xsl:param name="action" select="$a:action"/>
    <func:result
      select="a:normalize-url($a:root-url,concat(
        a:format-action($action),
        $a:preferences-url-params
      ))"/>
  </func:function>
  
  <func:function name="a:format-url-method">
    <xsl:param name="method"/> 
    <xsl:param name="url" select="$a:current-url-wo-ext"/>
    <xsl:param name="action" select="$a:action"/> 
    <func:result
      select="a:normalize-url(
        a:format-url-file-ext($url,$method),
        concat(
          a:format-action($action),
          $a:mode-param,
          $a:preferences-url-params,
          $a:session-url-param
        )
      )"/>
  </func:function>
  
  <func:function name="a:format-request">
    <xsl:param name="action" select="'view'"/>   
    <xsl:param name="url" select="$a:root-url"/> 
    <func:result
      select="a:normalize-url($url,concat(
        a:format-action($action),
        $a:common-url-params
      ))"/>
  </func:function>
  
  <func:function name="a:format-repeat-request">  
    <xsl:param name="action" select="$a:action"/>
    <xsl:param name="url" select="$a:current-url"/> 
    <func:result
      select="a:normalize-url($url,concat(
        a:format-action($action),
        $a:common-url-params,
        '&amp;no-referrer=1'
      ))"/>
  </func:function>
  
  <func:function name="a:format-reset-request">  
    <xsl:param name="action" select="'view'"/>
    <xsl:param name="url" select="$a:current-url"/>
    <func:result
      select="a:normalize-url($url,concat(
        a:format-action($action),
        $a:common-url-params,
        '&amp;no-referrer=1'
      ))"/>
  </func:function>
  
  <func:function name="a:format-request-new-mode"> 
    <xsl:param name="mode" select="$a:mode"/> 
    <xsl:param name="action" select="$a:action"/>
    <xsl:param name="url" select="$a:current-url"/>
    <func:result
      select="a:normalize-url($url,concat(
        a:format-action($action),
        a:format-mode($mode)
      ))"/>
  </func:function>
  
   <func:function name="a:format-form-request">  
    <xsl:param name="action" select="$a:action"/>
    <xsl:param name="url" select="$a:current-url"/>
    <func:result
      select="a:normalize-url($url,concat(
        a:format-action($action),
        $a:mode-param,
        $a:preferences-url-params,
        $a:session-url-param
      ))"/>
  </func:function>
  
  <func:function name="a:format-node-action">
    <xsl:param name="action" select="'view'"/>  
    <func:result
      select="a:normalize-url($a:current-url,concat(
        a:format-action($action),
        $a:common-url-params
      ))"/>
  </func:function>
  
  <func:function name="a:format-node-uri">
    <xsl:param name="uri"/>
    <xsl:param name="action" select="'view'"/>  
    <func:result
      select="a:normalize-url('/node/',concat(
        '&amp;method=',$a:method,'&amp;furi=',url:quote($uri),
        a:format-action($action),
        $a:common-url-params
      ))"/>
  </func:function>  
  
  <func:function name="a:format-root-action">
    <xsl:param name="action" select="'view'"/> 
    <func:result
      select="a:normalize-url($a:root-url,concat(
        a:format-action($action),
        $a:common-url-params
      ))"/>
  </func:function>    
  
  <func:function name="a:format-preferences-url">
    <xsl:param name="theme"/>   
    <xsl:param name="locale"/>   
    <xsl:param name="timezone"/>
    <func:result
      select="a:normalize-url($a:current-url,concat(
        a:format-action($a:action),
        a:format-theme($theme),
        a:format-locale($locale),
        a:format-timezone($timezone),
        $a:session-url-param
      ))"/>
  </func:function>  
  
  <func:function name="a:format-url-action-type"> 
    <xsl:param name="action-type"/> 
    
    <func:result
      select="a:normalize-url($a:current-url,concat(
        'action-type=',$action-type,
        $a:common-url-params
      ))"/>
  </func:function>  
  
  <!-- This function assumes that the base-url param contains atleast 1 param.
       A more advance function needs to check for this.
  -->
  <func:function name="a:format-url-ext">
    <xsl:param name="base-url"/>
    <xsl:param name="name"/>
    <xsl:param name="val"/>  
    <!-- we're cheating here - should verify that base-url already contains params -->    
    <xsl:choose>
      <xsl:when test="contains($base-url,'?')">
        <func:result
          select="concat($base-url,
            '&amp;',$name,'=',$val)"/>
      </xsl:when>
      <xsl:otherwise>
        <func:result
          select="concat($base-url,
            '?',$name,'=',$val)"/>
      </xsl:otherwise>
    </xsl:choose> 
    
  </func:function>
    
  <func:function name="a:format-stylesheet-path">
    <xsl:param name="url"/>
    <func:result select="$url"/>
  </func:function>
  
  <func:function name="a:format-static-url">
    <xsl:param name="url"/>
    <func:result select="$url"/>
  </func:function>
   
</xsl:stylesheet>
